import ratios from '../filter_ratios';
import { expect } from 'chai';
import sinon from 'sinon';

describe('ratios(req, panel, series)', () => {

  let panel;
  let series;
  let req;
  beforeEach(() => {
    panel = {
      time_field: 'timestamp'
    };
    series = {
      id: 'test',
      split_mode: 'terms',
      terms_size: 10,
      terms_field: 'host',
      metrics: [
        {
          id: 'metric-1',
          type: 'filter_ratio',
          numerator: 'errors',
          denominator: '*',
          metric_agg: 'avg',
          field: 'cpu'
        }
      ]
    };
    req = {
      payload: {
        timerange: {
          min: '2017-01-01T00:00:00Z',
          max: '2017-01-01T01:00:00Z'
        }
      }
    };
  });

  it('calls next when finished', () => {
    const next = sinon.spy();
    ratios(req, panel, series)(next)({});
    expect(next.calledOnce).to.equal(true);
  });

  it('returns filter ratio aggs', () => {
    const next = doc => doc;
    const doc = ratios(req, panel, series)(next)({});
    expect(doc).to.eql({
      aggs: {
        test: {
          aggs: {
            timeseries: {
              aggs: {
                'metric-1': {
                  bucket_script: {
                    buckets_path: {
                      denominator: 'metric-1-denominator>metric',
                      numerator: 'metric-1-numerator>metric',
                    },
                    script: 'params.numerator != null && params.denominator != null &&' +
                    ' params.denominator > 0 ? params.numerator / params.denominator : 0'
                  }
                },
                'metric-1-denominator': {
                  aggs: {
                    metric: {
                      avg: {
                        field: 'cpu'
                      }
                    }
                  },
                  filter: {
                    query_string: {
                      analyze_wildcard: true,
                      query: '*'
                    }
                  },
                },
                'metric-1-numerator': {
                  aggs: {
                    metric: {
                      avg: {
                        field: 'cpu'
                      }
                    }
                  },
                  filter: {
                    query_string: {
                      analyze_wildcard: true,
                      query: 'errors'
                    }
                  }
                }
              }
            }
          }
        }
      }
    });

  });

  it('returns empty object when field is not set', () => {
    delete series.metrics[0].field;
    const next = doc => doc;
    const doc = ratios(req, panel, series)(next)({});
    expect(doc).to.eql({
      aggs: {
        test: {
          aggs: {
            timeseries: {
              aggs: {
                'metric-1': {
                  bucket_script: {
                    buckets_path: {
                      denominator: 'metric-1-denominator>metric',
                      numerator: 'metric-1-numerator>metric',
                    },
                    script: 'params.numerator != null && params.denominator != null &&' +
                    ' params.denominator > 0 ? params.numerator / params.denominator : 0'
                  }
                },
                'metric-1-denominator': {
                  aggs: { metric: {} },
                  filter: {
                    query_string: {
                      analyze_wildcard: true,
                      query: '*'
                    }
                  },
                },
                'metric-1-numerator': {
                  aggs: { metric: {} },
                  filter: {
                    query_string: {
                      analyze_wildcard: true,
                      query: 'errors'
                    }
                  }
                }
              }
            }
          }
        }
      }
    });

  });
});



